home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / littlest / littl-st.lha / test.st < prev    next >
Text File  |  1993-08-10  |  3KB  |  105 lines

  1. *
  2. *
  3. * Little Smalltalk, version 2
  4. * Written by Tim Budd, Oregon State University, July 1987
  5. *
  6. *  a few test cases.
  7. *
  8. * to use, first file in this file, then pas all to an instance of
  9. *    class Test, for example using the messages
  10. *
  11. *    File new; fileIn: 'test.st'
  12. *     Test new all
  13. *
  14. Class Test Object
  15. Class One Object
  16. Class Two One
  17. Class Three Two
  18. Class Four Three
  19. Methods One 'all'
  20.         test
  21.                 ^ 1
  22. |
  23.     result1
  24.                 ^ self test
  25. ]
  26. Methods Two 'all'
  27.         test
  28.                 ^ 2
  29. ]
  30. Methods Three 'all'
  31.         result2
  32.                 ^ self result1
  33. |
  34.     result3
  35.                 ^ super test
  36. ]
  37. Methods Four 'all'
  38.         test
  39.                 ^ 4
  40. ]
  41. Methods Test 'all'
  42.     all
  43.         self super.
  44.         self conversions.
  45.         self collections.
  46.         self factorial.
  47.         self filein.
  48.         'all tests completed' print
  49. |
  50.     conversions
  51.         " test a few conversion routines "
  52.         ( (#abc == #abc asString asSymbol) and: [
  53.         ($A == $A asInteger asCharacter) and: [
  54.         (12 == 12 asDigit digitValue) and: [
  55.         (237 == 237 asString asInteger) and: [
  56.         (43 = 43 asFloat truncated) and: [
  57.         $A == ($A asString at: 1) ] ] ] ] ] )
  58.             ifFalse: [^ smalltalk error: 'conversion failure'].
  59.         'conversion test passed' print.
  60. |
  61.     collections
  62.         " test the collection classes a little"
  63.         ( (#(1 2 3 3 2 4 2) asSet = #(1 2 3 4) asSet) and: [
  64.         (#(1 5 3 2 4) sort asArray = #(1 2 3 4 5)) and: [
  65.         (1 "(#+ respondsTo occurrencesOf: Float)" = 1) and: [
  66.         ('First' < 'last') ] ] ] )
  67.             ifFalse: [^smalltalk error: 'collection failure'].
  68.         'collection test passed' print.
  69. |
  70.     factorial    | t |
  71.         t <- [:x | (x = 1) ifTrue: [ 1 ] 
  72.                 ifFalse: [ x * (t value: x - 1) ] ].
  73.         ((t value: 5) = 5 factorial)
  74.             ifFalse: [ smalltalk error: 'factorial failure'].
  75.         'factorial test passed' print
  76. |
  77.     filein
  78.         File new; name: 'queen.st'; open: 'r'; fileIn.
  79.         "(globalNames includesKey: #Queen )
  80.             ifFalse: [ smalltalk error: 'fileIn failure']."
  81.         'file in test passed' print.
  82.         self queen
  83. |
  84.     super2         | x1 x2 x3 x4 |
  85.                 x1 <- One new.
  86.                 x2 <- Two new.
  87.                 x3 <- Three new.
  88.                 x4 <- Four new.
  89.         ^ List new; addLast: x1 test;
  90.             addLast: x1 result1;
  91.             addLast: x2 test;
  92.             addLast: x2 result1;
  93.             addLast: x3 test;
  94.                     addLast: x4 result1;
  95.                     addLast: x3 result2;
  96.             addLast: x4 result2;
  97.                     addLast: x3 result3;
  98.                     addLast: x4 result3
  99. |
  100.     super
  101.         (self super2 asArray = #(1 1 2 2 2 4 2 4 2 2) )
  102.             ifTrue: ['super test passed' print]
  103.             ifFalse: [ smalltalk error: 'super test failed']
  104. ]
  105.